package lgkj.meicqt20.ui;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import lgkj.meicqt20.R;
import lgkj.meicqt20.adapter.GameAdapter;
import lgkj.meicqt20.adapter.NewsAdapter;
import lgkj.meicqt20.bean.GameEnitity;
import lgkj.meicqt20.bean.MessageEvent;
import lgkj.meicqt20.bean.NewItemBean;
import lgkj.meicqt20.bean.ShopEnitity;
import lgkj.meicqt20.utils.AllKindsOfConstant;
import lgkj.meicqt20.utils.HttpUtils;
import lgkj.meicqt20.view.BannerViewpager;

/**
 * Created by Administrator on 2019/1/18 0018.
 */

public class LifefallowPlayActivity extends BaseActivity {
    @BindView(R.id.mdxy)
    ImageView mdxy;
    @BindView(R.id.btn_news)
    ImageButton btnNews;
    @BindView(R.id.game_corner)
    ImageButton gameCorner;
    @BindView(R.id.btn_shop)
    ImageButton btnShop;
    @BindView(R.id.video_btn)
    LinearLayout videoBtn;
    @BindView(R.id.jin_xi)
    ImageView jinXi;
    @BindView(R.id.iv_code)
    ImageView ivCode;
    @BindView(R.id.video_little_banner)
    BannerViewpager videoLittleBanner;
    @BindView(R.id.gv_game)
    GridView gvGame;
    @BindView(R.id.lv_news_list)
    ListView lvNewsList;
    private List<NewItemBean> mListNewsBeans;
    private NewsAdapter adapter;
    private GameAdapter gameAdapter;
    private int gamePostion;
    private int topPosition = 0;
    private GameEnitity enitity;
    private ShopEnitity shopEnitity;
    private boolean isData = false;
    private int position = 0;
    private ImageButton[] btnTop;

    @Override
    public int setContentView() {
        return R.layout.activity_life_fallow;
    }

    @Override
    public void init() {
        activitiesList.add(this);
        displayDifferen();
        adapter = new NewsAdapter(LifefallowPlayActivity.this);
        lvNewsList.setAdapter(adapter);
        gameAdapter = new GameAdapter(LifefallowPlayActivity.this);
        gvGame.setAdapter(gameAdapter);
    }

    @Override
    public void initData() {
        bandViewInit(new initBannersDataResult() {
            @Override
            public void resultSuccess(List<String> list) {
                videoLittleBanner.setData(list);
            }
        });
        HttpUtils.getQRCode(LifefallowPlayActivity.this, ivCode);
        btnTop = new ImageButton[]{btnNews, gameCorner, btnShop};
        initNews();
        initPlay();
        getshop();
        select();
    }

    /**
     * 新闻
     *
     * @param order
     */
    public void fragmentDealOrder(String order) {
        switch (order) {
            case "up":
                if (isData && (position + 1) > 0) {
                    position--;
                    adapter.setmPositon(position);
                }
                break;
            case "down":
                if (isData && (position + 1) < adapter.getCount()) {
                    position++;
                    adapter.setmPositon(position);
                }
                break;
            case "right":
                topPosition = 1;
                lvNewsList.setVisibility(View.GONE);
                gvGame.setVisibility(View.VISIBLE);
                gamePostion = -1;
                if (enitity != null && enitity.getData() != null)
                    gameAdapter.setData(enitity.getData());
                gameAdapter.setPosition(gamePostion);
                gameAdapter.notifyDataSetChanged();
                gvGame.smoothScrollToPosition(0, 500);
                break;
            case "left":
                topPosition = 0;
                lvNewsList.setVisibility(View.VISIBLE);
                gvGame.setVisibility(View.GONE);
                break;
            case "back":
                finish();
                break;
            case "sure":
                if (topPosition == 0) {
                    Intent intent = new Intent(LifefallowPlayActivity.this, NewsDetailsActivity.class);
                    try {
                        intent.putExtra("news_id", adapter.getItem(position).getNews_id() + "");
                    } catch (Exception e) {
                        Log.e(TAG, "fragmentDealOrder: " + e.getMessage());
                    }
                    startActivity(intent);
                }
                break;
        }
        if (adapter.getCount() > 0) {
            lvNewsList.smoothScrollToPositionFromTop(position, 0, 20);
            adapter.notifyDataSetChanged();
        }
        gvGame.setSelection(gamePostion);
        gameAdapter.notifyDataSetChanged();
        Log.d(TAG, "order:" + order + ",position:" + position);
    }


    /**
     * 游戏
     * isGame   true
     *
     * @param order
     */
    private void gameOrder(String order) {
        switch (order) {
            case "sure":
                if (topPosition == 1) {
                    startActivity(new Intent(LifefallowPlayActivity.this,PlayActivity.class));
                }
                break;
            case "up":
                if (gamePostion < 3) {
                    gamePostion = -1;
                } else {
                    gamePostion = gamePostion - 4;
                }
                break;
            case "back":
                finish();
                break;
            case "down":
                if (gamePostion < gameAdapter.getCount()) {
                    if (gamePostion <= -1) {
                        gamePostion = 0;
                    } else {
                        gamePostion = gamePostion + 4;
                    }
                    if ((gamePostion + 1) >= gameAdapter.getCount()) {
                        gamePostion = gameAdapter.getCount() - 1;
                    }
                }
                break;
            case "right":
                if (gamePostion >= 0 && gameAdapter.getCount() > 0) {
                    if ((gamePostion + 1) < gameAdapter.getCount()) {
                        gamePostion++;
                    }
                } else {
                    topPosition = 2;
                    lvNewsList.setVisibility(View.VISIBLE);
                    if (shopEnitity != null && shopEnitity.getData() != null) {
                        gameAdapter.setShopData(shopEnitity.getData());
                    }
                }
                break;
            case "left":
                if (gamePostion >= 0) {
                    if (gamePostion > 0) {
                        gamePostion--;
                    }
                } else {
                    topPosition = 0;
                    shopPosition = -1;
                    lvNewsList.setVisibility(View.VISIBLE);
                    gvGame.setVisibility(View.GONE);
                }
                break;

        }
        gameAdapter.setPosition(gamePostion);
        gvGame.smoothScrollToPositionFromTop(gamePostion, 0);
        gameAdapter.notifyDataSetChanged();

    }

    /**
     * 二维码
     *
     * @param order
     */
    private int shopPosition = -1;

    private void shopOrder(String order) {
        switch (order) {
            case "up":
                if (shopPosition < 3) {
                    shopPosition = -1;
                } else {
                    shopPosition = shopPosition - 4;
                }
                break;
            case "down":
                if (shopPosition < gameAdapter.getCount()) {
                    if (shopPosition <= -1) {
                        shopPosition = 0;
                    } else {
                        shopPosition = shopPosition + 4;
                    }
                    if ((shopPosition + 1) >= gameAdapter.getCount()) {
                        shopPosition = gameAdapter.getCount() - 1;
                    }
                }
                break;
            case "right":
                if (shopPosition >= 0) {
                    if ((shopPosition + 1) < gameAdapter.getCount()) {
                        shopPosition++;
                    }
                }
                break;
            case "left":
                if (shopPosition >= 0 && gameAdapter.getCount() > 0) {
                    if (shopPosition > 0) {
                        shopPosition--;
                    }
                } else {
                    topPosition = 1;
                    gamePostion = -1;
                    lvNewsList.setVisibility(View.GONE);
                    gvGame.setVisibility(View.VISIBLE);
                    if (enitity != null && enitity.getData() != null)
                        gameAdapter.setData(enitity.getData());
                }
                break;
            case "back":
                finish();
                break;
            case "sure":
                break;
        }
        gameAdapter.setPosition(shopPosition);
        gvGame.smoothScrollToPositionFromTop(shopPosition, 0);
        gameAdapter.notifyDataSetChanged();

        Log.i(TAG, "shopOrder: " + order + ",shopPosition:" + shopPosition);
    }

    @Override
    public void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
        Log.d(TAG, "onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(MessageEvent messageEvent) {
        Log.i(TAG, "socket: " + messageEvent.toString());
        if ("control".equals(messageEvent.getTagName())) {
            String info = messageEvent.getMessage();
            if (topPosition == 0) {
                fragmentDealOrder(info);
            } else if (topPosition == 1) {
                gameOrder(info);
            } else {
                shopOrder(info);
            }
            //二维码
            select();
        }
    }

    private void select() {
        for (int i = 0; i < btnTop.length; i++) {
            if (topPosition == i) {
                btnTop[i].setSelected(true);
            } else {
                btnTop[i].setSelected(false);
            }
        }
    }

    /**
     * 新闻
     */
    private void initNews() {
        Map<String, String> newMap = new HashMap();
        newMap.put("news_type", "1");
        HttpUtils.getHttp(newMap, AllKindsOfConstant.getNewList, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("status") == 1) {
                    mListNewsBeans = new Gson().fromJson(jsonObject.optString("data"), new TypeToken<ArrayList<NewItemBean>>() {
                    }.getType());
                }

                Log.i(TAG, "----" + mListNewsBeans.size());
                if (mListNewsBeans != null && mListNewsBeans.size() > 0) {
                    adapter.setData(mListNewsBeans);
                    isData = true;
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void errorHttp(String msg) {
            }
        });

    }

    /**
     * 游戏
     */
    private void initPlay() {
        Map<String, String> playMap = new HashMap();
        HttpUtils.getHttp(playMap, AllKindsOfConstant.getgame, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                try {
                    if (jsonObject.getInt("status") == 1) {
                        enitity = new Gson().fromJson(jsonObject.toString(), GameEnitity.class);
                        if (enitity.getData() != null) {
                            gameAdapter.setData(enitity.getData());
                            gameAdapter.notifyDataSetChanged();
                        }
                    }
                } catch (JSONException e) {
                    Log.d(TAG, "successHttp: " + e.getMessage());
                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }

    /**
     * 二维码
     */
    private void getshop() {
        Map<String, String> shopMap = new HashMap();
//        shopMap.put("mac", "38:a2:8c:6b:1e:4d");
        HttpUtils.getHttp(shopMap, AllKindsOfConstant.getshop, new HttpUtils.HttpCallBack() {

            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                try {
                    if (jsonObject.getInt("status") == 1) {
                        shopEnitity = new Gson().fromJson(jsonObject.toString(), ShopEnitity.class);
                        if (shopEnitity.getData() != null) {
                            gameAdapter.setShopData(shopEnitity.getData());
                            gameAdapter.notifyDataSetChanged();
                        }
                    }
                } catch (JSONException e) {
                    Log.d(TAG, "successHttp: " + e.getMessage());
                }
            }

            @Override
            public void errorHttp(String msg) {
            }
        });
    }

}
